package web.services;

import web.bean.Qualification;
import web.dao.QualificationDao;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 关注招标信息资质服务 Created by 愤怒的番茄 on 2014/10/8.
 */
@Service
public class QualificationServices {

    @Autowired
    private  QualificationDao qualificationDao;

    private final static Logger LOGGER = Logger.getLogger("QualificationServices.class");



    //    缓存资质信息到map中，可以通过Id查询到资质
    private  Map<Integer, Qualification> qualificationKeyMap = new HashMap<Integer, Qualification>();
    //缓存资质列表
	private  List<Map<String,Object>> qualificationList = new ArrayList<Map<String,Object>>();

        //对资质经行初始化
        public  Map<Integer, Qualification> getQualificationMap() {
            if(qualificationKeyMap.size()==0) {
                init();
                new Thread(){
                    @Override
                    public void run() {
                        try {
//                            Thread.sleep(1000*60*60*24);
                            Thread.sleep(1000*60);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
//                        System.out.println("资质开始更新");
                        clear();
                    }
                }.start();
            }
            return qualificationKeyMap;
        }
//        static {
//            init();
//        }

	private  void init() {
		// 获取key列表
		qualificationList = qualificationDao
				.findQualificationList();
		// 获取全部
//将资质存入静态的map中
		for (Map<String,Object> map : qualificationList) {
			setQualificationMap(map);
		}

	}

    public String getUserSearchQualification(String qualifcationIds){
        if(qualifcationIds.equals("")){
            return "";
        }
        String []str=qualifcationIds.trim().split(",");
        StringBuffer sbf=new StringBuffer();
        Map<Integer, Qualification> map=getQualificationMap();
        for (String s : str) {
            if(!str[str.length-1].equals(s)) {
                sbf.append(map.get(Integer.parseInt(s)).getName() + ",");
            }else{
                sbf.append(map.get(Integer.parseInt(s)).getName());
            }
        }
        return sbf.toString().trim();
    }
// 资质信息存到Map中
	private   void setQualificationMap(Map<String,Object> map) {
		int id = Integer.parseInt(map.get("id").toString());

		Qualification qualification = new Qualification();
		qualification.setId( Integer.parseInt(map.get("id").toString()));
		qualification.setName(map.get("name").toString());
		qualification.setSeachTerm(new String[] {map.get("key").toString()});
		
		
		List<Map<String,Object>> list = qualificationDao.findQualificationOther(qualification.getName());
		for (Map<String,Object> map2 : list) {
			qualification.setSeachTerm((String[]) ArrayUtils.add(qualification.getSeachTerm(), map2.get("key").toString()));
		}
		qualificationKeyMap.put(id, qualification);
		
	}

	/**
	 * 清除缓存
	 */
	public  void clear() {
		qualificationKeyMap.clear();
		qualificationList.clear();
		init();
	}

    /**
     * 获取资质列表
     * @return
     */
	public  List<Map<String,Object>> getQualificationList() {
        if(qualificationList.size()==0){
            clear();
            new Thread(){
                @Override
                public void run() {
                    try {
//                            Thread.sleep(1000*60*60*24);
                        Thread.sleep(1000*60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    System.out.println("资质开始更新");
                    clear();
                }
            }.start();
        }
		return qualificationList;
	}

    /**
     * 获取资质Map
     * @return
     */
//    public static Map<Integer, Qualification> getQualificationMap() {
//        return qualificationKeyMap;
//    }
    /**
     * 获取资质Map
     * @return
     */
//	public Map<Integer, Qualification> getQualificationMap() {
//		return qualificationKeyMap;
//	}

	public  void insert(Qualification qualification) {

	}

    /**
     * 显示资质，如果资质中包含用户设置的资质，给其添加高亮
     * @param qualification
     * @param list
     * @return
     */
    public String getQualificationForShow(String qualification,List<String> list) {
        if (StringUtils.isBlank(qualification)){
            return "";
        }
        StringBuffer qualificationStringBuffer = new StringBuffer();




        String[] qualificationStrs = qualification.split(" ");
//       给包含用户设置的资质添加高亮
        for (String qualificationStr : qualificationStrs) {
            if(list.size()>0&&list.contains(qualificationStr)){
                qualificationStringBuffer.append("<span class=\'highlight hot\'>"+qualificationStr+"</span>,");
            }else{
                qualificationStringBuffer.append(qualificationStr+",");
            }


        }
         return qualificationStringBuffer.toString().substring(0,qualificationStringBuffer.length()-1);
    }

    public String getQualificationForShow(String qualification){
        return getQualificationForShow(qualification,new ArrayList<String>());
    }

}
